package com.ckx.controller;

import com.alibaba.nacos.common.model.RestResult;
import com.ckx.base.PageResult;
import com.ckx.dao.OrderMapper;
import com.ckx.dao.entity.OrderDO;
import com.ckx.dto.OrderQueryDTO;
import com.ckx.dto.OrderRespDTO;
import com.ckx.service.IOrderService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hutool.core.data.id.IdUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private IOrderService orderService;

    @GetMapping("add")
    public RestResult<Long> add(Long id) {
        OrderDO orderDO = new OrderDO();
        orderDO.setId(id == null ? IdUtil.getSnowflakeNextId() : id);
        orderDO.setProductName(IdUtil.fastSimpleUUID());
        orderDO.setCreateTime(new Date());
        orderMapper.insert(orderDO);
        return RestResult.<Long>builder().withData(orderDO.getId()).build();
    }

    @GetMapping("get")
    public RestResult<OrderDO> get(Long id) throws Exception {
        OrderDO userDO = orderMapper.selectById(id);
        return RestResult.<OrderDO>builder().withData(userDO).build();
    }

    @GetMapping("page")
    public RestResult<PageResult<OrderRespDTO>> pageList(@Validated OrderQueryDTO orderQueryDTO) {
        return RestResult.<PageResult<OrderRespDTO>>builder().withData(orderService.orderPage(orderQueryDTO)).build();
    }

}
